| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276 | 1
1
1
2149
366
22
344
1783
1
702
600
53
1
796
1
370
291
79
79
79
79
79
54
54
54
54
54
54
54
54
54
54
54
54
54
54
54
54
54
10
54
54
639
639
639
4
639
195
444
47
397
54
647
53
594
54
458
54
11
11
11
9
2
2
4
2
2
2
11
11
54
11
1
10
9
1
10
10
54
647
647
647
4
647
54
54
54
54
59
59
3
56
56
56
54
66
66
66
66
66
40
40
40
66
22
22
19
19
11
11
22
11
66
54
54
54
53
19
19
53
181
121
72
10
10
181
2
181
181
181
181
181
181
181
181
181
181
18
181
181
181
| (function ( angular ) {
'use strict';
angular.module( 'treeControl', [] )
.directive( 'treecontrol', ['$compile', function( $compile ) {
/**
* @param cssClass - the css class
* @param addClassProperty - should we wrap the class name with class=""
*/
function classIfDefined(cssClass, addClassProperty) {
if (cssClass) {
if (addClassProperty)
return 'class="' + cssClass + '"';
else
return cssClass;
}
else
return "";
}
function ensureDefault(obj, prop, value) {
if (!obj.hasOwnProperty(prop))
obj[prop] = value;
}
return {
restrict: 'EA',
require: "treecontrol",
transclude: true,
scope: {
treeModel: "=",
selectedNode: "=?",
expandedNodes: "=?",
onSelection: "&",
onNodeToggle: "&",
options: "=?",
orderBy: "@",
reverseOrder: "@",
filterExpression: "=?",
filterComparator: "=?"
},
controller: ['$scope', function( $scope ) {
function defaultIsLeaf(node) {
return !node[$scope.options.nodeChildren] || node[$scope.options.nodeChildren].length === 0;
}
function defaultEquality(a, b) {
if (a === undefined || b === undefined)
return false;
a = angular.copy(a);
a[$scope.options.nodeChildren] = [];
b = angular.copy(b);
b[$scope.options.nodeChildren] = [];
return angular.equals(a, b);
}
$scope.options = $scope.options || {};
ensureDefault($scope.options, "nodeChildren", "children");
ensureDefault($scope.options, "dirSelectable", "true");
ensureDefault($scope.options, "injectClasses", {});
ensureDefault($scope.options.injectClasses, "ul", "");
ensureDefault($scope.options.injectClasses, "li", "");
ensureDefault($scope.options.injectClasses, "liSelected", "");
ensureDefault($scope.options.injectClasses, "iExpanded", "");
ensureDefault($scope.options.injectClasses, "iCollapsed", "");
ensureDefault($scope.options.injectClasses, "iLeaf", "");
ensureDefault($scope.options.injectClasses, "label", "");
ensureDefault($scope.options.injectClasses, "labelSelected", "");
ensureDefault($scope.options, "equality", defaultEquality);
ensureDefault($scope.options, "isLeaf", defaultIsLeaf);
$scope.expandedNodes = $scope.expandedNodes || [];
$scope.expandedNodesMap = {};
for (var i=0; i < $scope.expandedNodes.length; i++) {
$scope.expandedNodesMap[""+i] = $scope.expandedNodes[i];
}
$scope.parentScopeOfTree = $scope.$parent;
$scope.headClass = function(node) {
var liSelectionClass = classIfDefined($scope.options.injectClasses.liSelected, false);
var injectSelectionClass = "";
if (liSelectionClass && ($scope.options.equality(this.node, $scope.selectedNode)))
injectSelectionClass = " " + liSelectionClass;
if ($scope.options.isLeaf(node))
return "tree-leaf" + injectSelectionClass;
if ($scope.expandedNodesMap[this.$id])
return "tree-expanded" + injectSelectionClass;
else
return "tree-collapsed" + injectSelectionClass;
};
$scope.iBranchClass = function() {
if ($scope.expandedNodesMap[this.$id])
return classIfDefined($scope.options.injectClasses.iExpanded);
else
return classIfDefined($scope.options.injectClasses.iCollapsed);
};
$scope.nodeExpanded = function() {
return !!$scope.expandedNodesMap[this.$id];
};
$scope.selectNodeHead = function() {
var expanding = $scope.expandedNodesMap[this.$id] === undefined;
$scope.expandedNodesMap[this.$id] = (expanding ? this.node : undefined);
if (expanding) {
$scope.expandedNodes.push(this.node);
}
else {
var index;
for (var i=0; (i < $scope.expandedNodes.length) && !index; i++) {
if ($scope.options.equality($scope.expandedNodes[i], this.node)) {
index = i;
}
}
Eif (index != undefined)
$scope.expandedNodes.splice(index, 1);
}
Eif ($scope.onNodeToggle)
$scope.onNodeToggle({node: this.node, expanded: expanding});
};
$scope.selectNodeLabel = function( selectedNode ){
if (selectedNode[$scope.options.nodeChildren] && selectedNode[$scope.options.nodeChildren].length > 0 &&
!$scope.options.dirSelectable) {
this.selectNodeHead();
}
else {
if ($scope.selectedNode != selectedNode) {
$scope.selectedNode = selectedNode;
}
else {
$scope.selectedNode = undefined;
}
Eif ($scope.onSelection)
$scope.onSelection({node: $scope.selectedNode});
}
};
$scope.selectedClass = function() {
var labelSelectionClass = classIfDefined($scope.options.injectClasses.labelSelected, false);
var injectSelectionClass = "";
if (labelSelectionClass && (this.node == $scope.selectedNode))
injectSelectionClass = " " + labelSelectionClass;
return (this.node == $scope.selectedNode)?"tree-selected" + injectSelectionClass:"";
};
//tree template
var template =
'<ul '+classIfDefined($scope.options.injectClasses.ul, true)+'>' +
'<li ng-repeat="node in node.' + $scope.options.nodeChildren + ' | filter:filterExpression:filterComparator | orderBy:orderBy:reverseOrder" ng-class="headClass(node)" '+classIfDefined($scope.options.injectClasses.li, true)+'>' +
'<i class="tree-branch-head" ng-class="iBranchClass()" ng-click="selectNodeHead(node)"></i>' +
'<i class="tree-leaf-head '+classIfDefined($scope.options.injectClasses.iLeaf, false)+'"></i>' +
'<div class="tree-label '+classIfDefined($scope.options.injectClasses.label, false)+'" ng-class="selectedClass()" ng-click="selectNodeLabel(node)" tree-transclude></div>' +
'<treeitem ng-if="nodeExpanded()"></treeitem>' +
'</li>' +
'</ul>';
this.template = $compile(template);
}],
compile: function(element, attrs, childTranscludeFn) {
return function ( scope, element, attrs, treemodelCntr ) {
scope.$watch("treeModel", function updateNodeOnRootScope(newValue) {
Eif (angular.isArray(newValue)) {
if (angular.isDefined(scope.node) && angular.equals(scope.node[scope.options.nodeChildren], newValue))
return;
scope.node = {};
scope.synteticRoot = scope.node;
scope.node[scope.options.nodeChildren] = newValue;
}
else {
if (angular.equals(scope.node, newValue))
return;
scope.node = newValue;
}
});
scope.$watchCollection('expandedNodes', function(newValue) {
var notFoundIds = 0;
var newExpandedNodesMap = {};
var $liElements = element.find('li');
var existingScopes = [];
// find all nodes visible on the tree and the scope $id of the scopes including them
angular.forEach($liElements, function(liElement) {
var $liElement = angular.element(liElement);
var liScope = $liElement.scope();
existingScopes.push(liScope);
});
// iterate over the newValue, the new expanded nodes, and for each find it in the existingNodesAndScopes
// if found, add the mapping $id -> node into newExpandedNodesMap
// if not found, add the mapping num -> node into newExpandedNodesMap
angular.forEach(newValue, function(newExNode) {
var found = false;
for (var i=0; (i < existingScopes.length) && !found; i++) {
var existingScope = existingScopes[i];
if (scope.options.equality(newExNode, existingScope.node)) {
newExpandedNodesMap[existingScope.$id] = existingScope.node;
found = true;
}
}
if (!found)
newExpandedNodesMap[notFoundIds++] = newExNode;
});
scope.expandedNodesMap = newExpandedNodesMap;
});
// scope.$watch('expandedNodesMap', function(newValue) {
//
// });
//Rendering template for a root node
treemodelCntr.template( scope, function(clone) {
element.html('').append( clone );
});
// save the transclude function from compile (which is not bound to a scope as apposed to the one from link)
// we can fix this to work with the link transclude function with angular 1.2.6. as for angular 1.2.0 we need
// to keep using the compile function
scope.$treeTransclude = childTranscludeFn;
}
}
};
}])
.directive("treeitem", function() {
return {
restrict: 'E',
require: "^treecontrol",
link: function( scope, element, attrs, treemodelCntr) {
// Rendering template for the current node
treemodelCntr.template(scope, function(clone) {
element.html('').append(clone);
});
}
}
})
.directive("treeTransclude", function() {
return {
link: function(scope, element, attrs, controller) {
if (!scope.options.isLeaf(scope.node)) {
angular.forEach(scope.expandedNodesMap, function (node, id) {
if (scope.options.equality(node, scope.node)) {
scope.expandedNodesMap[scope.$id] = scope.node;
scope.expandedNodesMap[id] = undefined;
}
});
}
if (scope.options.equality(scope.node, scope.selectedNode)) {
scope.selectedNode = scope.node;
}
// create a scope for the transclusion, whos parent is the parent of the tree control
scope.transcludeScope = scope.parentScopeOfTree.$new();
scope.transcludeScope.node = scope.node;
scope.transcludeScope.$parentNode = (scope.$parent.node === scope.synteticRoot)?null:scope.$parent.node;
scope.transcludeScope.$index = scope.$index;
scope.transcludeScope.$first = scope.$first;
scope.transcludeScope.$middle = scope.$middle;
scope.transcludeScope.$last = scope.$last;
scope.transcludeScope.$odd = scope.$odd;
scope.transcludeScope.$even = scope.$even;
scope.$on('$destroy', function() {
scope.transcludeScope.$destroy();
});
scope.$treeTransclude(scope.transcludeScope, function(clone) {
element.empty();
element.append(clone);
});
}
}
});
})( angular );
|